import unittest

import os
import sys
import logging
import warnings
import threading
import time
import json
path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)

from contract_market.initialize import config  # NOQA
from contract_market.initialize.config import *  # NOQA
from contract_market.core.actor import *  # NOQA
from contract_market.core.core import *  # NOQA
from contract_market.server.engine import EventEngine  # NOQA
from contract_market.common.common import EventSystemEngine  # NOQA
from contract_market.common.constant import *  # NOQA
from contract_market.common.rpc.rpc import RPCClient, RPC  # NOQA
from contract_market.base.event import Event , EventData # NOQA


def init_engine():
    event = EventCenter()
    engine = EventEngine(event)
    engine.add_engine(EventSystemEngine)
    print(event.obtain_events())
    return event, engine

def get_uuid():
    import uuid
    get_timestamp_uuid = uuid.uuid1()  # 根据 时间戳生成 uuid , 保证全球唯一
    # print("len---- ", len(str(get_timestamp_uuid.int)))
    return get_timestamp_uuid.int


class TestRPC(unittest.TestCase):

    @unittest.skip
    def test_init(self):
        event = Event(XT_CANCEL_ORDER_EVENT, None)
        client = RPC()
        res = client.describe()
        print("[res] test_init ", res)
        res = client.get_account_info()
        print("[res] test_init ", res)
        pass
    
    @unittest.skip
    def test_describe(self):
        event, engine = init_engine()
        event_data = EventData()
        data = {"wait":True}
        data = json.dumps(data)
        event_data.data = data
        e = Event(EVENT_DESCRIBE, event_data)
        engine.send(e)
        print(e.event_data.result)

    # @unittest.skip
    def test_get_account_info(self):
        event, engine = init_engine()

        event_data = EventData()

        data = {"wait":True, "XT":{}}
        data = json.dumps(data)
        event_data.data = data
        e = Event(XT_ACCOUNT_INFO_EVENT, event_data)
        engine.send(e)
        
        print(e.event_data.result)

    @unittest.skip
    def test_get_balance(self):
        event, engine = init_engine()
        data = {
            "xt": {
                "accesskey": "c15c7459-b65a-43af-8165-7ac0ae853154",
                "secretkey": "939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"
            },
            "wait":True,
            }
        data = json.dumps(data)

        event_data = EventData()
        event_data.data = data
        e = Event(XTFUTURE_BALANCE_EVENT, event_data)
        engine.send(e)
        print(e.event_data.result)
    
    # @unittest.skip
    def test_send_order(self):
        event, engine = init_engine()
        data = {
            "xtfuture": {
                '_extra':{
                    "app_name": "test",
                    "user_name": "zhangsan",
                    "note": "---",
                },
                'accesskey': 'b1000aa0-9119-4ce7-a963-e7a0fe0055cb',
                'secretkey': 'b9992066608e6b84b531cbf1bf36b06e9a79add8',
                'market': "xrp_usdt",
                'price': 0.47,
                'amount': 500,
                'order_side': "BUY",
                'order_type': "LIMIT",
                'position_side': "LONG",
                'timeInForce': 'GTC'},

            "uuid": get_uuid(),
            'timeout': 1,
        }
        data = json.dumps(data)
        event_data = EventData()
        event_data.data = data
        e = Event(XTFUTURE_SEND_ORDER_EVENT, event_data)
        engine.send(e)
        print(e.event_data.result)

    @unittest.skip
    def test_cancel_order(self):
        time.sleep(1)
        event, engine = init_engine()
        order = [
            {"id": 129686790855502464, "market": "xx_usdt"},
            {"id": 129686790855502465, "market": "xx_usdt"},
        ]
        data = {
            "xtfuture": {
                '_extra':{
                    "app_name": "test",
                    "user_name": "zhangsan",
                    "note": "---",
                },
                "data": order,
                # "accesskey": "c15c7459-b65a-43af-8165-7ac0ae853154",
                # "secretkey": "939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"},
                'accesskey': 'b1000aa0-9119-4ce7-a963-e7a0fe0055cb',
                'secretkey': 'b9992066608e6b84b531cbf1bf36b06e9a79add8'},
            "uuid": get_uuid(),
            'timeout': 10,
        }
        data = json.dumps(data)

        event_data = EventData()
        event_data.data = data
        e = Event(XTFUTURE_CANCEL_ORDER_EVENT, event_data)
        engine.send(e)
        print(e.event_data.result)

    @unittest.skip
    def test_cancel_order(self):
        for i in range(3):
            time.sleep(1)
            event, engine = init_engine()
            order = [
                {"id": 129686790855502464, "market": "xx_usdt"},
            ]
            data = {
                "xtfuture": {
                    '_extra':{
                        "app_name": "test",
                        "user_name": "zhangsan",
                        "note": "---",
                    },
                    "data": order,
                    # "accesskey": "c15c7459-b65a-43af-8165-7ac0ae853154",
                    # "secretkey": "939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"},
                    'accesskey': 'b1000aa0-9119-4ce7-a963-e7a0fe0055cb',
                    'secretkey': 'b9992066608e6b84b531cbf1bf36b06e9a79add8'},
                "uuid": get_uuid(),
                'timeout': 10,
            }
            data = json.dumps(data)

            event_data = EventData()
            event_data.data = data
            e = Event(XTFUTURE_CANCEL_ORDER_EVENT, event_data)
            engine.send(e)
            print(e.event_data.result)

    @unittest.skip
    def test_open_orders(self):
        event, engine = init_engine()
        order = [
            {"id": 129686790855502464, "market": "btc_usdt"},
        ]
        data = {
            "xtfuture": {
                '_extra':{
                    "app_name": "test",
                    "user_name": "zhangsan",
                    "note": "---",
                },
                "data": order,
                "accesskey": "c15c7459-b65a-43af-8165-7ac0ae853154",
                "secretkey": "939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"},
            "uuid": get_uuid(),
            'timeout': 10,
        }
        data = json.dumps(data)

        event_data = EventData()
        event_data.data = data
        e = Event(XTFUTURE_CANCEL_ORDER_EVENT, event_data)
        engine.send(e)
        print(e.event_data.result)

#     def test_send_order_thread(self):
#         event, engine = init_engine()
#         t1 = threading.Thread(target=test_send_order, args=(engine,), daemon=True)
#         t1.start()
#         t2 = threading.Thread(target=test_send_order, args=(engine,), daemon=True)
#         t2.start()
#         t2.join()

# def test_send_order(engine):
#     while 1:
#         data = {
#             # "xt": {
#             # "market": "btc_usdt",
#             # "price": 24100,
#             # '_extra':{
#             #     "app_name": "test",
#             #     "user_name": "zhangsan",
#             #     "note": "---",
#             # },
#             # "type": 1,
#             # "entrustType": "0",
#             # "number": 0.1,
#             # "accesskey": "c15c7459-b65a-43af-8165-7ac0ae853154",
#             # "secretkey": "939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"},

#             "xtfuture": {
#                 '_extra':{
#                     "app_name": "test",
#                     "user_name": "zhangsan",
#                     "note": "---",
#                 },
#                 'accesskey': 'b1000aa0-9119-4ce7-a963-e7a0fe0055cb',
#                 'secretkey': 'b9992066608e6b84b531cbf1bf36b06e9a79add8',
#                 'market': "btc_usdt",
#                 'price': 21500,
#                 'amount': 100,
#                 'order_side': "BUY",
#                 'order_type': "LIMIT",
#                 'position_side': "LONG",
#                 'timeInForce': 'GTC'},

#             "uuid": get_uuid(),
#             'timeout': 10,
#         }
#         time.sleep(0.1)
#         data = json.dumps(data)

#         event_data = EventData()
#         event_data.data = data
#         e = Event(XTFUTURE_SEND_ORDER_EVENT, event_data)
#         engine.send(e)
#         print(e.event_data.result)


if __name__ == "__main__":
    unittest.main()
    pass
